package org.wu.framework.authorization.platform.application.impl;

import org.wu.framework.lazy.orm.web.plus.stereotype.LazyApplication;
import org.wu.framework.authorization.platform.application.SysRolePermissionApplication;
import org.springframework.web.bind.annotation.*;
import org.wu.framework.web.response.Result;
import org.wu.framework.web.response.ResultFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.wu.framework.authorization.platform.domain.model.sys.role.permission.SysRolePermission;
import org.wu.framework.authorization.platform.application.command.sys.role.permission.SysRolePermissionRemoveCommand;
import org.wu.framework.authorization.platform.application.command.sys.role.permission.SysRolePermissionStoryCommand;
import org.wu.framework.authorization.platform.application.command.sys.role.permission.SysRolePermissionUpdateCommand;
import org.wu.framework.authorization.platform.application.command.sys.role.permission.SysRolePermissionQueryListCommand;
import org.wu.framework.authorization.platform.application.command.sys.role.permission.SysRolePermissionQueryOneCommand;
import org.wu.framework.authorization.platform.application.assembler.SysRolePermissionDTOAssembler;
import org.wu.framework.authorization.platform.application.dto.SysRolePermissionDTO;
import java.util.stream.Collectors;
import jakarta.annotation.Resource;
import org.wu.framework.authorization.platform.domain.model.sys.role.permission.SysRolePermissionRepository;
import java.util.List;
import org.wu.framework.lazy.orm.database.lambda.domain.LazyPage;
import org.wu.framework.authorization.platform.application.command.sys.role.permission.SysRolePermissionExcelExport;
import org.wu.framework.authorization.platform.application.command.sys.role.permission.SysRolePermissionExcelImport;
/**
 * describe 角色权限关系 
 *
 * @author Jia wei Wu
 * @date 2025/08/03 01:55 下午
 * @see org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyApplicationImpl 
 **/
@LazyApplication
public class SysRolePermissionApplicationImpl implements SysRolePermissionApplication {

    @Resource
    SysRolePermissionRepository sysRolePermissionRepository;
    /**
     * describe 新增角色权限关系
     *
     * @param sysRolePermissionStoryCommand 新增角色权限关系     
     * @return {@link Result<SysRolePermission>} 角色权限关系新增后领域对象     
     
     * @author Jia wei Wu
     * @date 2025/08/03 01:55 下午
     **/

    @Override
    public Result<SysRolePermission> story(SysRolePermissionStoryCommand sysRolePermissionStoryCommand) {
        SysRolePermission sysRolePermission = SysRolePermissionDTOAssembler.INSTANCE.toSysRolePermission(sysRolePermissionStoryCommand);
        return sysRolePermissionRepository.story(sysRolePermission);
    }
    /**
     * describe 批量新增角色权限关系
     *
     * @param sysRolePermissionStoryCommandList 批量新增角色权限关系     
     * @return {@link Result<List<SysRolePermission>>} 角色权限关系新增后领域对象集合     
     
     * @author Jia wei Wu
     * @date 2025/08/03 01:55 下午
     **/

    @Override
    public Result<List<SysRolePermission>> batchStory(List<SysRolePermissionStoryCommand> sysRolePermissionStoryCommandList) {
        List<SysRolePermission> sysRolePermissionList = sysRolePermissionStoryCommandList.stream().map( SysRolePermissionDTOAssembler.INSTANCE::toSysRolePermission).collect(Collectors.toList());
        return sysRolePermissionRepository.batchStory(sysRolePermissionList);
    }
    /**
     * describe 更新角色权限关系
     *
     * @param sysRolePermissionUpdateCommand 更新角色权限关系     
     * @return {@link Result<SysRolePermission>} 角色权限关系领域对象     
     
     * @author Jia wei Wu
     * @date 2025/08/03 01:55 下午
     **/

    @Override
    public Result<SysRolePermission> updateOne(SysRolePermissionUpdateCommand sysRolePermissionUpdateCommand) {
        SysRolePermission sysRolePermission = SysRolePermissionDTOAssembler.INSTANCE.toSysRolePermission(sysRolePermissionUpdateCommand);
        return sysRolePermissionRepository.story(sysRolePermission);
    }

    /**
     * describe 查询单个角色权限关系
     *
     * @param sysRolePermissionQueryOneCommand 查询单个角色权限关系     
     * @return {@link Result<SysRolePermissionDTO>} 角色权限关系DTO对象     
     
     * @author Jia wei Wu
     * @date 2025/08/03 01:55 下午
     **/

    @Override
    public Result<SysRolePermissionDTO> findOne(SysRolePermissionQueryOneCommand sysRolePermissionQueryOneCommand) {
        SysRolePermission sysRolePermission = SysRolePermissionDTOAssembler.INSTANCE.toSysRolePermission(sysRolePermissionQueryOneCommand);
        return sysRolePermissionRepository.findOne(sysRolePermission).convert(SysRolePermissionDTOAssembler.INSTANCE::fromSysRolePermission);
    }

    /**
     * describe 查询多个角色权限关系
     *
     * @param sysRolePermissionQueryListCommand 查询多个角色权限关系     
     * @return {@link Result<List<SysRolePermissionDTO>>} 角色权限关系DTO对象     
     
     * @author Jia wei Wu
     * @date 2025/08/03 01:55 下午
     **/

    @Override
    public Result<List<SysRolePermissionDTO>> findList(SysRolePermissionQueryListCommand sysRolePermissionQueryListCommand) {
        SysRolePermission sysRolePermission = SysRolePermissionDTOAssembler.INSTANCE.toSysRolePermission(sysRolePermissionQueryListCommand);
        return sysRolePermissionRepository.findList(sysRolePermission)        .convert(sysRolePermissions -> sysRolePermissions.stream().map(SysRolePermissionDTOAssembler.INSTANCE::fromSysRolePermission).collect(Collectors.toList())) ;
    }

    /**
     * describe 分页查询多个角色权限关系
     *
     * @param sysRolePermissionQueryListCommand 分页查询多个角色权限关系     
     * @return {@link Result<LazyPage<SysRolePermissionDTO>>} 分页角色权限关系DTO对象     
     
     * @author Jia wei Wu
     * @date 2025/08/03 01:55 下午
     **/

    @Override
    public Result<LazyPage<SysRolePermissionDTO>> findPage(int size,int current,SysRolePermissionQueryListCommand sysRolePermissionQueryListCommand) {
        SysRolePermission sysRolePermission = SysRolePermissionDTOAssembler.INSTANCE.toSysRolePermission(sysRolePermissionQueryListCommand);
        return sysRolePermissionRepository.findPage(size,current,sysRolePermission)        .convert(page -> page.convert(SysRolePermissionDTOAssembler.INSTANCE::fromSysRolePermission))            ;
    }

    /**
     * describe 删除角色权限关系
     *
     * @param sysRolePermissionRemoveCommand 删除角色权限关系     
     * @return {@link Result<SysRolePermission>} 角色权限关系     
     
     * @author Jia wei Wu
     * @date 2025/08/03 01:55 下午
     **/

    @Override
    public Result<SysRolePermission> remove(SysRolePermissionRemoveCommand sysRolePermissionRemoveCommand) {
     SysRolePermission sysRolePermission = SysRolePermissionDTOAssembler.INSTANCE.toSysRolePermission(sysRolePermissionRemoveCommand);
     return sysRolePermissionRepository.remove(sysRolePermission);
    }

    /**
     * describe 导出角色权限关系
     *
     * @param sysRolePermissionQueryListCommand 导出角色权限关系     
     * @return {@link List<SysRolePermissionExcelExport>} 角色权限关系     
     
     * @author Jia wei Wu
     * @date 2025/08/03 01:55 下午
     **/

    @Override
    public List<SysRolePermissionExcelExport> exportExcel(SysRolePermissionQueryListCommand sysRolePermissionQueryListCommand) {
     SysRolePermission sysRolePermission = SysRolePermissionDTOAssembler.INSTANCE.toSysRolePermission(sysRolePermissionQueryListCommand);
        return sysRolePermissionRepository.findList(sysRolePermission)        .applyOther(sysRolePermissions -> sysRolePermissions.stream().map(SysRolePermissionDTOAssembler.INSTANCE::fromSysRolePermissionToExcelExport).collect(Collectors.toList())) ;
    }

    /**
     * describe 导入角色权限关系
     *
     * @param sysRolePermissionExcelImportList 导入角色权限关系     
     * @return {@link Result<SysRolePermissionExcelImport>} 角色权限关系     
     
     * @author Jia wei Wu
     * @date 2025/08/03 01:55 下午
     **/

    @Override
    public Result<List<SysRolePermissionExcelImport>> importExcel(List<SysRolePermissionExcelImport> sysRolePermissionExcelImportList) {
        List<SysRolePermission> sysRolePermissionList = sysRolePermissionExcelImportList.stream().map( SysRolePermissionDTOAssembler.INSTANCE::toSysRolePermission).collect(Collectors.toList());
        sysRolePermissionRepository.batchStory(sysRolePermissionList);
        return ResultFactory.successOf(sysRolePermissionExcelImportList);
    }
}